﻿using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sharp.UnusedReferences
{
	class VsSelectionEvents : IVsSelectionEvents, IDisposable
	{
		#region Fields

		int intActive = 0;
		private IVsMonitorSelection monitorSelectionService = null;
		private uint pdwCookie;
		private readonly uint CookieSolutionExists;
		private readonly uint CookieSolutionHasSingleProject;
		private readonly uint CookieSolutionHasMultipleProjects;
		private readonly uint CookieDebugging;
		public bool SolutionExists
		{
			get;
			private set;
		}
		public bool SolutionHasSingleProject
		{
			get;
			private set;
		}
		public bool SolutionHasMultipleProjects
		{
			get;
			private set;
		}
		public bool Debugging
		{
			get;
			private set;
		}

		#endregion

		#region Constructors
		public VsSelectionEvents()
		{
			Guid GuidSolutionExists = VSConstants.UICONTEXT_SolutionExists;
			Guid GuidSolutionHasSingleProject = VSConstants.UICONTEXT_SolutionHasSingleProject;
			Guid GuidSolutionHasMultipleProjects = VSConstants.UICONTEXT_SolutionHasMultipleProjects;
			Guid GuidDebugging = VSConstants.UICONTEXT_Debugging;
			monitorSelectionService = ServiceProvider.GlobalProvider.GetService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
			if(monitorSelectionService != null)
			{
				monitorSelectionService.GetCmdUIContextCookie(ref GuidSolutionExists, out CookieSolutionExists);
				monitorSelectionService.GetCmdUIContextCookie(ref GuidSolutionHasSingleProject, out CookieSolutionHasSingleProject);
				monitorSelectionService.GetCmdUIContextCookie(ref GuidSolutionHasMultipleProjects, out CookieSolutionHasMultipleProjects);
				monitorSelectionService.GetCmdUIContextCookie(ref GuidDebugging, out CookieDebugging);
				monitorSelectionService.AdviseSelectionEvents(this, out pdwCookie);
			}
		}
		#endregion

		protected uint SolutionSubscriptionCookie = 0;

		#region IVsSelectionEvents Members

		int IVsSelectionEvents.OnCmdUIContextChanged(uint dwCmdUICookie, int fActive)
		{
			if(monitorSelectionService != null)
			{
				monitorSelectionService.IsCmdUIContextActive(CookieSolutionExists, out intActive);
				SolutionExists = Convert.ToBoolean(intActive);
				monitorSelectionService.IsCmdUIContextActive(CookieSolutionHasSingleProject, out intActive);
				SolutionHasSingleProject = Convert.ToBoolean(intActive);
				monitorSelectionService.IsCmdUIContextActive(CookieSolutionHasMultipleProjects, out intActive);
				SolutionHasMultipleProjects = Convert.ToBoolean(intActive);
				monitorSelectionService.IsCmdUIContextActive(CookieDebugging, out intActive);
				Debugging = Convert.ToBoolean(intActive);
			}

			//if(active != 0 && SolutionSubscriptionCookie == 0)
			//{
			//    // The solution is loaded. Now we can get hold of our selection events. 
			//    IVsSolution solution = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution;
			//    //Debug.Assert(solution != null);

			//    solution.AdviseSolutionEvents(TWToolbarPackage.SolutionEvents, out SolutionSubscriptionCookie);
			//}
			return VSConstants.S_OK;
		}

		public int OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
		{
			return VSConstants.S_OK;
		}

		public int OnSelectionChanged(IVsHierarchy pHierOld, uint itemidOld, IVsMultiItemSelect pMISOld, ISelectionContainer pSCOld, IVsHierarchy pHierNew, uint itemidNew, IVsMultiItemSelect pMISNew, ISelectionContainer pSCNew)
		{
			return VSConstants.S_OK;
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if(monitorSelectionService != null && pdwCookie != 0)
			{
				monitorSelectionService.UnadviseSelectionEvents(pdwCookie);
			}
		}

		#endregion
	}
}
